package com.baitai.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baitai.common.constant.CacheConstants;
import com.baitai.common.core.redis.RedisCache;
import com.baitai.common.utils.DateUtils;
import com.baitai.common.utils.StringUtils;
import com.baitai.system.domain.IssueTaskFile;
import com.baitai.system.domain.ReceiveTask;
import com.baitai.system.mapper.ReceiveTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baitai.system.mapper.IssueTaskMapper;
import com.baitai.system.domain.IssueTask;
import com.baitai.system.service.IIssueTaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务下发Service业务层处理
 * 
 * @author xx
 * @date 2024-07-14
 */
@Service
public class IssueTaskServiceImpl implements IIssueTaskService 
{
    @Autowired
    private IssueTaskMapper issueTaskMapper;
    @Autowired
    private ReceiveTaskMapper receiveTaskRelationMapper;

    /**
     * 查询任务下发
     *
     * @param id 任务下发主键
     * @return 任务下发
     */
    @Override
    public IssueTask selectIssueTaskById(Long id)
    {
        return issueTaskMapper.selectIssueTaskById(id);
    }

    /**
     * 查询任务下发列表
     *
     * @param issueTask 任务下发
     * @return 任务下发
     */
    @Override
    public List<IssueTask> selectIssueTaskList(IssueTask issueTask)
    {
        return issueTaskMapper.selectIssueTaskList(issueTask);
    }

    /**
     * 新增任务下发
     *
     * @param issueTask 任务下发
     * @return 结果
     */
    @Transactional
    @Override
    public int insertIssueTask(IssueTask issueTask)
    {
        issueTask.setCreateTime(DateUtils.getNowDate());
        issueTask.setRemainingNumber(issueTask.getMaxNumber());
        int rows = issueTaskMapper.insertIssueTask(issueTask);
        insertIssueTaskFile(issueTask);
        return rows;
    }

    /**
     * 修改任务下发
     *
     * @param issueTask 任务下发
     * @return 结果
     */
    @Transactional
    @Override
    public int updateIssueTask(IssueTask issueTask)
    {
        issueTask.setUpdateTime(DateUtils.getNowDate());
        issueTaskMapper.deleteIssueTaskFileByIssueTaskId(issueTask.getId());
        insertIssueTaskFile(issueTask);
        return issueTaskMapper.updateIssueTask(issueTask);
    }

    /**
     * 批量删除任务下发
     *
     * @param ids 需要删除的任务下发主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIssueTaskByIds(Long[] ids)
    {
        issueTaskMapper.deleteIssueTaskFileByIssueTaskIds(ids);
        return issueTaskMapper.deleteIssueTaskByIds(ids);
    }

    /**
     * 删除任务下发信息
     *
     * @param id 任务下发主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIssueTaskById(Long id)
    {
        issueTaskMapper.deleteIssueTaskFileByIssueTaskId(id);
        return issueTaskMapper.deleteIssueTaskById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param issueTask 任务下发对象
     */
    public void insertIssueTaskFile(IssueTask issueTask)
    {
        List<IssueTaskFile> issueTaskFileList = issueTask.getIssueTaskFileList();
        Long id = issueTask.getId();
        if (StringUtils.isNotNull(issueTaskFileList))
        {
            List<IssueTaskFile> list = new ArrayList<IssueTaskFile>();
            for (IssueTaskFile issueTaskFile : issueTaskFileList)
            {
                issueTaskFile.setIssueTaskId(id);
                list.add(issueTaskFile);
            }
            if (list.size() > 0)
            {
                issueTaskMapper.batchIssueTaskFile(list);
            }
        }
    }

    @Transactional
    @Override
    public int receiveTask(Long userId,Long taskId) {
        IssueTask issueTask = issueTaskMapper.selectIssueTaskById(taskId);
        if(StringUtils.greaterThan(issueTask.getRemainingNumber(),"1")){
            issueTask.setRemainingNumber(StringUtils.minus(issueTask.getRemainingNumber(),"1"));
            issueTaskMapper.updateIssueTask(issueTask);
        }else {
            return 1;
        }
        ReceiveTask receiveTaskRelation = receiveTaskRelationMapper.selectReceiveTaskByUserIdAndTaskId(userId,taskId);
        if(receiveTaskRelation!=null){
            return 2;
        }else {
            receiveTaskRelation = new ReceiveTask();
            receiveTaskRelation.setUserId(userId);
            receiveTaskRelation.setTaskId(taskId);
            receiveTaskRelation.setAuditStatus("1");
            receiveTaskRelation.setCreateTime(DateUtils.getNowDate());
            receiveTaskRelationMapper.insertReceiveTask(receiveTaskRelation);
        }
        return 0;
    }

    @Override
    public int cancelReceiveTask(Long userId, Long taskId) {
        return receiveTaskRelationMapper.deleteReceiveTaskByUserIdAndTaskId(userId,taskId);
    }

    @Override
    public List<IssueTask> selectIssueTaskList(Long userId, IssueTask issueTask) {
        List<IssueTask> result = issueTaskMapper.selectIssueTaskList(issueTask);
        List<ReceiveTask> receiveTasks =receiveTaskRelationMapper.selectReceiveTaskListByUserId(userId);
        for (IssueTask task : result) {
            for (ReceiveTask receiveTask : receiveTasks) {
                if(receiveTask.getTaskId().equals(task.getId())){
                    task.setTaskStatus(receiveTask.getAuditStatus());
                }
            }
        }
        return result;
    }
}
